package com.example.lte.repo;

import com.example.lte.entity.RoleUserEntity;
import com.example.lte.entity.RoleUserId;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * RoleUserId为联合主键
 */
@Repository
public interface RoleUserRelRepo extends JpaRepository<RoleUserEntity, RoleUserId> {

    List<RoleUserEntity> findByUserId(Long userId);


    /**
     * Hibernate: delete from role_user where user_id in (? , ? , ?)
     *
     * @param userIds Long[]
     * @return int
     */
    @Transactional
    @Modifying(clearAutomatically = true)
    @Query("delete from RoleUserEntity where userId in ?1")
    int deleteByUserIds(Long[] userIds);

    List<RoleUserEntity> findByRoleId(Long roleId);

    RoleUserEntity findByRoleIdAndUserId(Long roleId , Long userId);
}
